import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/themes/layout/index'
import store from '../store'
import school from './modules/school'
import init from './modules/init'
import student from './modules/student'
import teacher from './modules/teacher'
import task from './modules/task'
import dorm from './modules/dorm'
import log from './modules/log'
import conectPage from './modules/conectPage'
import role from './modules/role'
import device from './modules/device'
Vue.use(Router)
// 路由实例
const router = new Router({
  scrollBehavior (to, from, savedPosition) { // 路由滚动行为
    if (savedPosition) {
        return savedPosition
    } else {
        const position = {}
        if (to.hash) {
            position.selector = to.hash
        }
        if (to.matched.some(m => m.meta.scrollToTop)) {
            position.x = 0
            position.y = 0
        }
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve(position)
            }, 100)
        })
    }
  },
  routes: [
    school,
    student,
    teacher,
    init,
    task,
    dorm,
    log,
    conectPage,
    role,
    device,
    {
      path: '/',
      redirect: '/init'
    },
    {
      path: '/home',
      name: '首页',
      component: () => import('@/themes/layout/index')
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('../themes/login/Login')
    },
    {
      path: '*',
      redirect: '/error/404'
    },
    {
      path: '/error/403',
      name: 'Error403',
      component: () => import('../themes/error/403')
    },
    {
      path: '/error/404',
      name: 'Error404',
      component: () => import('../themes/error/404')
    }
  ]
})
router.beforeEach((to, from, next) => {
  store
    .dispatch('common/GgetTokenByPC', '')
    .then(res => {
      next()
    })
    .catch(error => {
      console.log(error)
      next()
    })  
})
router.afterEach((to, from) => {
  // window.document.title = to.meta.title
})
export default router
